package com.hua.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.pojo.Blog;
import com.hua.pojo.BlogCategory;
import com.hua.result.R;
import com.hua.service.BlogCategoryService;
import com.hua.service.BlogService;
import com.hua.utils.HuaUtil;
import com.hua.vo.ArticleData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 博客
 * @author ONE-X
 * @date 2020/11/13
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

	@Autowired
	private BlogService blogService;
	@Autowired
	private BlogCategoryService blogCategoryService;

	/**
	 * 访问博客首页
	 */
	@GetMapping("/index")
	public String toBlogIndex(){
		return "/blog/list";
	}
	/**
	 * 获取博客 类型 以及当前类型的总条数 基本信息
	 */
	@ResponseBody
	@PostMapping("/basic")
	public String blogBasic(){
		// 获取全部博客分类信息
		List<BlogCategory> blogCategories = blogCategoryService.list();
		String[] blogCategoriesArray = new String[blogCategories.size()];
		AtomicInteger index = new AtomicInteger();
		blogCategories.forEach((v)->{
			blogCategoriesArray[index.getAndIncrement()] = v.getCategory();
		});
		return JSON.toJSONString(blogCategoriesArray);
	}

	/**
	 * 获取全部信息
	 * @return
	 */
	@ResponseBody
	@PostMapping("/list/{index}")
	public String findBlog(@PathVariable String index, String categoryName){
		if("".equals(categoryName) || "all".equals(categoryName)){
			categoryName = null;
		}
		IPage<Blog> page = new Page<>();
		page.setSize(10);
		page.setCurrent(Long.parseLong(index));
		page = blogService.findAllBlog(page,categoryName,null);
		R r = R.ok();
		Map<String, Object> map = new HashMap<>();
		map.put("blogList",page.getRecords());
		map.put("index",page.getCurrent());
		map.put("total",page.getTotal());
		r.data(map);
		return JSON.toJSONString(r);
	}

	@GetMapping("/write")
	public String toWrite(){
		return "blog/write";
	}

	@ResponseBody
	@PostMapping("/getCategory")
	public String getCategory(){
		List<BlogCategory> list = blogCategoryService.list();
		String[] category = new String[list.size()];
		AtomicInteger atomicInteger = new AtomicInteger();
		list.forEach((v)->{
			category[atomicInteger.getAndIncrement()] = v.getCategory();
		});
		return JSON.toJSONString(category);
	}

	@ResponseBody
	@PostMapping("/write")
	public String writeBlog(@RequestBody ArticleData articleData){
		blogService.addBlog(articleData);
		return JSON.toJSONString(R.ok());
	}


	@GetMapping("/read/{bid}")
	public String readBlog(@PathVariable String bid, Model model){
		// 查看博客
		Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
		model.addAttribute("blog",blog);
		return "blog/read";
	}

	@ResponseBody
	@PostMapping("/blogList")
	public String userList(){
		List<Blog> blogList = blogService.list(new QueryWrapper<Blog>().eq("author_id", HuaUtil.returnAuthentication()));
		return JSON.toJSONString(blogList);
	}
}
